#define _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"
void slinit(sl* s)
{
	s->arr = NULL;
	s->capacity = s->size = 0;
}

void slcheckcapacity(sl* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		sldatatype* temp = (sldatatype*)realloc(ps->arr, newcapacity * sizeof(sldatatype));
		if (temp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = temp;
		ps->capacity = newcapacity;
	}
}

//void slcheckcapacity(sl* ps)
//{
//	if (ps->size == ps->capacity)
//	{
//		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
//		sldatatype* temp = (sldatatype*)realloc(ps->arr, newcapacity * sizeof(sldatatype));
//		if (temp == NULL)
//		{
//			perror("realloc fail!");
//			exit(1);
//		}
//		ps->arr = temp;
//		ps->capacity = newcapacity;
//	}
//}

void sldestory(sl* ps)
{
	if (ps->arr)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void slpushback(sl* ps, sldatatype x)
{
	assert(ps);
	slcheckcapacity(ps);
	ps->arr[ps->size] = x;
	ps->size++;
}

void slpushfront(sl* ps, sldatatype x)
{
	assert(ps);
	slcheckcapacity(ps);
	for (int i = ps->size; i > 0; i--)
	{
		ps->arr[i] = ps->arr[i - 1];
	}
	ps->arr[0] = x;
	ps->size++;
}

void slpopback(sl* ps)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}

void slpopfront(sl* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

void slprint(sl s)
{
	for (int i = 0; i < s.size; i++)
	{
		printf("%d ", s.arr[i]);
	}
	printf("\n");
}

void slinsert(sl* ps, int pos, sldatatype x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	slcheckcapacity(ps);
	for (int i = ps->size - 1; i >= pos; i--)
	{
		ps->arr[i + 1] = ps->arr[i];
	}
	ps->arr[pos] = x;
	ps->size++;
}

void slerase(sl* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos + 1; i < ps->size; i++)
	{
		ps->arr[i - 1] = ps->arr[i];
	}
	ps->size--;
}

int slfind(sl* ps, sldatatype x)
{
	assert(ps);
	for (int i = 0; i < ps->size; i++)
	{
		if (ps->arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}